<?php

namespace app\admin\controller\course;

use app\common\controller\Backend;
use app\common\model\Classday2;
use app\common\model\ClassDayDetail;
use app\common\model\School;
use GuzzleHttp\Client;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 课节
 *
 * @package app\admin\controller
 */
class Classday extends Backend
{

    /**
     * @var \app\common\model\ClassDay
     */
    protected $model = null;
    protected $searchFields = 'id,name';
    protected $noNeedRight = ['searchlist'];
    protected $schoolLimit = true;
    protected $modelValidate = true;
    protected $modelSceneValidate = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('ClassDay');
    }

    /**
     * 查看
     */
    public function index()
    {
        $filter = $this->request->request('filter');
        $filter = json_decode($filter, true);

        $school_id = 0;
        if (isset($filter['school_id'])) {
            $school_id = $filter['school_id'];
        }

        if ($this->request->isAjax()) {
            $class_days = $this->model->where('school_id', $school_id)->select();

            $days = [
                '1' => 'monday',
                '2' => 'tuesday',
                '3' => 'wednesday',
                '4' => 'thursday',
                '5' => 'friday',
                '6' => 'saturday',
                '7' => 'sunday',
            ];
            $data = [];
            foreach ($class_days as $key => $value) {
                if (isset($days[$value['day']])) {
                    $data[$value['number']]['number'] = $value['number'];
                    $data[$value['number']][$days[$value['day']]]['id'] = $value['id'];
                    $data[$value['number']][$days[$value['day']]]['day'] = $value['day'];
                    $data[$value['number']][$days[$value['day']]]['value'] = $value['start_time_text'] . '-' . $value['end_time_text'];
                }
            }

            $result = array("total" => count($data), "rows" => array_values($data));

            return json($result);
        }

        $this->assignconfig('admin', ['school_id' => $school_id]);
        return $this->view->fetch();
    }

    public function add($id = null)
    {
        $school = School::get($id);

        if (!$school) {
            echo '请先选择学校';
            die;
        }

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {

                $validate = Validate('ClassDay');
                $res = $validate->scene('add')->check($params);
                if (!$res) {
                    $this->error($validate->getError());
                }

                $school_id = $this->auth->school_id;

                $insert_data = [];
                for ($day = 0; $day < 7; $day++) {
                    $start_time = $params['start_time'];
                    for ($number = 0; $number < $params['count']; $number++) {
                        $end_time = date('H:i', strtotime($start_time . "+{$params['class_time']} minute"));
                        $insert_data[] = [
                            'number' => $number + 1,
                            'day' => $day + 1,
                            'start_time' => $start_time,
                            'end_time' => $end_time,
                            'school_id' => $school->id,
                            'school_id' => $school_id,
                        ];
                        $start_time = date('H:i', strtotime($end_time . "+{$params['break_time']} minute"));
                    }
                }

                $this->model->where('school_id', $school->id)->where('school_id', $school_id)->delete();
                $this->model->saveAll($insert_data);

                $this->success('设置成功');
            }
        }
        $this->view->assign("school", $school);

        return $this->view->fetch();
    }

    public function set()
    {
        $id = $this->request->request('id');
        $class_day = $this->model->get($id);

        if ($this->request->isAjax()) {

            $start_time = $this->request->request('start_time');
            $end_time = $this->request->request('end_time');

            $class_day->start_time = $start_time;
            $class_day->end_time = $end_time;
            $class_day->save();

            $this->success('修改成功');
        }

        $this->view->assign("id", $class_day->id);
        $this->view->assign("start_time", $class_day->start_time);
        $this->view->assign("end_time", $class_day->end_time);

        return $this->view->fetch();
    }


}
